<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

<script type="text/javascript">
	var form_url = base.contextPath+"/sysmgr/dept/add.do" ;
	var company, dept, u1, u2, u3, u4 ;
	var simple = [[
		'fontset', '|', 'forecolor', 'backcolor',
		'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify','|', 
		'emotion', 'scrawl', '|', 'snapscreen', 'insertimage', 'attachment', '|', 'source', 'preview'
	]];
	$(function() {
		company = $("#company").combotree();
		dept = $("#dept").combotree();
		
		u1 = new UE.ui.Editor({ toolbars : simple });
		u1.render('u1');
		u2 = new UE.ui.Editor({ toolbars : simple });
		u2.render('u2');
		u3 = new UE.ui.Editor({ toolbars : simple });
		u3.render('u3');
		u4 = new UE.ui.Editor({ toolbars : simple });
		u4.render('u4');
		
		
		
		u1.addListener('ready', function(e) {
			//编辑，加载表单数据
			if($('input[name=id]').val().length > 0) {
				form_url = base.contextPath+"/sysmgr/dept/edit.do" ;
				$.post(base.contextPath+"/sysmgr/dept/get.do", {id:$('input[name=id]').val()}, function(result) {
					if (result.id != undefined) {
						$('form').form('load', {
							'id' : result.id,
							'name' : result.name,
							'tel' : result.tel,
							'fax' : result.fax,
							'position' : result.position,
							'pid' : result.pid,
							'company_id' : result.company_id
						});
						u1.setContent(result.duty);
						u2.setContent(result.year_target);
						u3.setContent(result.work_plan);
						u4.setContent(result.remark);
						
						company.combotree({
							url : base.contextPath+"/sysmgr/company/doNotNeedAuth_tree.do",
							editable: false, lines:true, required: true,
							onLoadSuccess: function(node, data) {
								if(data && data.length>0){
									company.combotree("setValue", result.company_id) ; 
								}
							},
							onChange: function(newValue, oldValue) { 
								dept.combotree({
				        			url : base.contextPath+"/sysmgr/dept/doNotNeedAuth_tree.do?company_id="+newValue,
				        			editable: false, lines:true,
				        			onLoadSuccess: function(node, data) {
				        				if(result.pid){
				        					dept.combotree("setValue", result.pid) ; 
				        				}
				        			}
				        	    });
				            }
					    });
					}
				}, 'json');
			} else {
				company.combotree({
					url : base.contextPath+"/sysmgr/company/doNotNeedAuth_tree.do",
					editable: false, lines:true, required: true,
					onLoadSuccess: function(node, data) {
						if(data && data.length>0){
							company.combotree("setValue", data[0].id) ; 
						}
					},
					onChange: function(newValue, oldValue) { 
						dept.combotree({
		        			url : base.contextPath+"/sysmgr/dept/doNotNeedAuth_tree.do?company_id="+newValue,
		        			editable: false, lines:true
		        	    });
		            }
			    });
			}
	 	} );
		
		
	});
	
	//提交表单数据
	var submitNow = function($d, $t) {
		$.post(form_url, $("#form").form("getData"), function(result) {
			if (result.status) {
				$t.tree('reload') ;
				alertify.success(result.msg);
				$.easyui.loaded(); $d.dialog("close") ;
			} else {
				$.easyui.loaded();$.messager.alert("错误", result.msg, "error");
			}
		}, 'json').error(function() {$.easyui.loaded();});
	};
	
	//验证表单
	var submitForm = function($d, $t) {
		if($('#form').form('validate')) {
			$.easyui.loading({ msg: "数据提交中，请稍等..." });
			submitNow($d, $t) ;
		}
	};
	
	
</script>

<form id="form" class="easyui-form form_container">
	<input type="hidden" name="id" value="${id}" />
	<input type="hidden" name="modifyName" value="${USER_SESSION.truename}" />
	<table class="tableform">
		<tr>
			<th>隶属单位：</th>
			<td><input id=company name="company_id" style="width:218px;height:30px;" /><a onClick="company.combotree('setValue','');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_clear'"></a></td>
			<th>上级部门：</th>
			<td><input id="dept" name="pid" style="width:218px;height:30px;" /><a onClick="dept.combotree('setValue','');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_clear'"></a></td>
		</tr>
		<tr>
			<th>部门名称：</th>
			<td><input name="name" class="easyui-validatebox" type="text" data-options="required: true,prompt: '名称'" /></td>
			<th>办公电话：</th>
			<td><input name="tel" class="easyui-validatebox" type="text" data-options="" /></td>
		</tr>
		<tr>
			<th>传真号码：</th>
			<td><input name="fax" class="easyui-validatebox" type="text" data-options="" /></td>
			<th>部门位置：</th>
			<td><input name="position" class="easyui-validatebox" type="text" data-options="" /></td>
		</tr>
		<tr>
			<th>部门职责：</th>
			<td colspan="3">
				<textarea id="u1" name="duty" style="width:565px;height:120px;"></textarea>
			</td>
		</tr>
		<tr>
			<th>年度目标：</th>
			<td colspan="3">
				<textarea id="u2" name="year_target" style="width:565px;height:120px;"></textarea>
			</td>
		</tr>
		<tr>
			<th>任务计划：</th>
			<td colspan="3">
				<textarea id="u3" name="work_plan" style="width:565px;height:120px;"></textarea>
			</td>
		</tr>
		<tr>
			<th>简介：</th>
			<td colspan="3">
				<textarea id="u4" name="remark" style="width:565px;height:220px;"></textarea>
			</td>
		</tr>
	</table>
</form>
